package com.zzdz.system.service.impl;

import com.baomidou.mybatisplus.core.conditions.query.LambdaQueryWrapper;
import com.zzdz.common.entity.QueryRequest;
import com.zzdz.common.exception.ZzdzException;
import com.zzdz.system.entity.Dict;
import com.zzdz.system.entity.DictItem;
import com.zzdz.system.mapper.DictItemMapper;
import com.zzdz.system.mapper.DictMapper;
import com.zzdz.system.service.IDictItemService;
import org.apache.commons.lang3.StringUtils;
import org.springframework.stereotype.Service;
import org.springframework.transaction.annotation.Transactional;
import org.springframework.transaction.annotation.Propagation;
import org.springframework.beans.factory.annotation.Autowired;

import com.baomidou.mybatisplus.core.conditions.query.QueryWrapper;
import com.baomidou.mybatisplus.core.metadata.IPage;
import com.baomidou.mybatisplus.extension.plugins.pagination.Page;
import com.baomidou.mybatisplus.extension.service.impl.ServiceImpl;

import java.util.Arrays;
import java.util.Date;
import java.util.List;

/**
 * 字典-子表 Service实现
 *
 * @author Zzdz
 * @date 2020-05-21 14:13:34
 */
@Service
@Transactional(propagation = Propagation.SUPPORTS, readOnly = true, rollbackFor = Exception.class)
public class DictItemServiceImpl extends ServiceImpl<DictItemMapper, DictItem> implements IDictItemService {

    @Autowired
    private DictItemMapper dictItemMapper;
    @Autowired
    private DictMapper dictMapper;

    @Override
    public IPage<DictItem> findDictItems(QueryRequest request, DictItem dictItem) {
        QueryWrapper<DictItem> queryWrapper = new QueryWrapper<>();
        queryWrapper.lambda()
            .like(dictItem.getId() != null, DictItem::getId, dictItem.getId())
            .like(dictItem.getDictId() != null, DictItem::getDictId, dictItem.getDictId())
            .like(StringUtils.isNotBlank(dictItem.getValue()), DictItem::getValue, dictItem.getValue())
            .like(StringUtils.isNotBlank(dictItem.getLable()), DictItem::getLable, dictItem.getLable())
            .like(StringUtils.isNotBlank(dictItem.getType()), DictItem::getType, dictItem.getType())
            .like(StringUtils.isNotBlank(dictItem.getDescription()), DictItem::getDescription, dictItem.getDescription())
            .like(StringUtils.isNotBlank(dictItem.getRemarks()), DictItem::getRemarks, dictItem.getRemarks());
        Page<DictItem> page = new Page<>(request.getPageNum(), request.getPageSize());
        return this.page(page, queryWrapper);
    }

    @Override
    public List<DictItem> findDictItems(DictItem dictItem) {
        QueryWrapper<DictItem> queryWrapper = new QueryWrapper<>();
        queryWrapper.lambda()
                .like(dictItem.getId() != null, DictItem::getId, dictItem.getId())
                .like(dictItem.getDictId() != null, DictItem::getDictId, dictItem.getDictId())
                .like(StringUtils.isNotBlank(dictItem.getValue()), DictItem::getValue, dictItem.getValue())
                .like(StringUtils.isNotBlank(dictItem.getLable()), DictItem::getLable, dictItem.getLable())
                .like(StringUtils.isNotBlank(dictItem.getType()), DictItem::getType, dictItem.getType())
                .like(StringUtils.isNotBlank(dictItem.getDescription()), DictItem::getDescription, dictItem.getDescription())
                .like(StringUtils.isNotBlank(dictItem.getRemarks()), DictItem::getRemarks, dictItem.getRemarks());
		return this.baseMapper.selectList(queryWrapper);
    }

    @Override
    @Transactional
    public void createDictItem(DictItem dictItem) {
        QueryWrapper<DictItem> queryWrapper = new QueryWrapper<>();
        queryWrapper.lambda().eq(dictItem.getDictId() != null, DictItem::getDictId, dictItem.getDictId())
                .eq(StringUtils.isNotBlank(dictItem.getValue()), DictItem::getValue, dictItem.getValue());
        List<DictItem> itemList = this.baseMapper.selectList(queryWrapper);
        if (itemList != null && itemList.size() > 0){
            throw new ZzdzException("已存在该值的字典类型");
        }
        Dict dict = dictMapper.selectById(dictItem.getDictId());
        dictItem.setType(dict.getType());
        dictItem.setCreateTime(new Date());
        this.save(dictItem);
    }

    @Override
    @Transactional
    public void updateDictItem(DictItem dictItem) {
        dictItem.setUpdateTime(new Date());
        this.saveOrUpdate(dictItem);
    }

    @Override
    @Transactional
    public void deleteDictItem(DictItem dictItem) {
        LambdaQueryWrapper<DictItem> wrapper = new LambdaQueryWrapper<>();
	    // TODO 设置删除条件
	    this.remove(wrapper);
	}

    @Override
    @Transactional
    public  void deleteDictItemByIds(String ids){
        if (StringUtils.isNotBlank(ids)){
            String[] idAttr = ids.split(",");
            dictItemMapper.deleteBatchIds(Arrays.asList(idAttr));
        }
    }

}
